package com.study.talk.ui.adapter.moment

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.talk.R
import com.study.talk.ui.adapter.base.BaseListAdapter
import com.study.talk.databinding.ImgUrlGridItemBinding
import com.study.talk.utils.extras.toImage

class ImageGridAdapter :
    BaseListAdapter<String, ImageGridAdapter.ViewHolder>() {
    class ViewHolder(val context: Context, val binding: ImgUrlGridItemBinding) :
        RecyclerView.ViewHolder(binding.root)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context, DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.img_url_grid_item, parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.imageUrl = getItem(position)
        // 设置图片网格的点击事件
        holder.binding.momentGridViewItemImage.setOnClickListener { _ ->
            // 启动图片预览活动
            holder.context.apply {
                toImage(currentList,position)
            }
        }
    }
}
